<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>Couchdbkit - Build an external handler</title>
    <!--[if IE]>

    <script>
        // allow IE to recognize HTMl5 elements
        document.createElement('section');
        document.createElement('article');
        document.createElement('aside');
        document.createElement('footer');
        document.createElement('header');
        document.createElement('nav');
        document.createElement('time');

    </script>
    <![endif]-->

    <link rel="stylesheet" href="/css/couchdbkit.css?20090615" type="text/css" media="screen, projection" />
    <link rel="stylesheet" href="/static/css/print.css?20090615" type="text/css" media="print" />
    <script type="text/javascript" src="/js/prettify.js"></script>
    
    <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="/feed.xml" />
    

    
        
</head>
<body onload="prettyPrint()">
    
    <div class="container">
        <header id="site_header">
            <h1><a href="/"><span>couchdbkit</span></a></h1>
            <ul id="main_nav">
                <li><a href="/">home</a></li>
                <li><a href="/blog/">news</a></li>
                <li><a href="/download.html">download</a></li>
                <li><a href="/docs/">documentation</a></li>
                <li><a href="/docs/api/">API</a></li>
            </ul>

        </header>
        
        
        
        
        <aside id="sidebar">
            <ul class="sidenav">
                <li><a href="/blog/">Latest news</a></li>
                <li><a href="/download.html">Download latest</a></li>
                <li><a href="/docs/gettingstarted.html">Getting started</a></li>
                <li><a href="/docs/faq.html">Frequently Asked Questions</a></li>
                <li><a href="/docs/about.html">About couchdbkit</a></li>
                <li><a href="/contact.html">Getting help with couchdbkit</a></li>
            </ul>
            <div class="info">
                <h3>Getting started</h3>
                <p>See the <a href="/download.html">download instructions</a> for information on getting couchdbkit, and read the <a href="/docs/gettingstarted.html">getting started</a> instructions to start programming CouchDB in your own application in less than 10 minutes.</p>
            </div>
            <div class="info">
                <h3>Get involved</h3>
                <ul>
                    <li><p><a href="http://webchat.freenode.net/?channels=couchdbkit">#couchdbkit IRC channel</a>: chat with other couchdbkit users</p></li>
                    <li><p><a href="http://github.com/benoitc/couchdbkit/issues">Ticket system</a>: report bugs and make feature requests</p></li>
                    <li><p><a href="http://groups.google.com/group/couchdbkit">Mailing-List</a>: The main list for help and follow changes.</p></li>
                </ul>
                        
            </div>
           
        </aside>
        
    
        
<section id="content">
    <article>
        <h1>Build an external handler</h1>
        	<p>Building an <a href="http://wiki.apache.org/couchdb/ExternalProcesse">external handler</a> with couchdbkit is easy. The only thing you have to do is to create an object inheriting from `couchdbkit.external` module. </p>

	<p>Here is a little snippet that shows you how to handle the <a href="http://wiki.apache.org/couchdb/ExternalProcesse#Example_External_Process">example external process from CouchDB wiki</a> with couchdbkit.</p>

	<p><pre class="code prettyprint">
 from couchdbkit.external import External
 import anyjson</p>

 class Test(External):

     def handle_line(self, line):
         self.send_response(200, 
             &#8220;got message external object %s&#8221; % anyjson.serialize(line),
             {&#8220;Content-type&#8221;: &#8220;text/plain&#8221;})

 if <i>name</i> == &#8220;<i>main</i>&#8220;:
     Test().run()
</pre>

	<p>All lines received by the external handler are handled in `handle_line` method of External instance. Then the `send_response` method can be used to send the response. It takes the http status code, body and headers as arguments. `This method is a wrapper around `write` method of your instance. </p>

	<p>`couchdbkit.External` interface has been used to build the wsgi external handler. Its <a href="http://bitbucket.org/benoitc/couchdbkit/src/5d7bb0ebf3f4/couchdbkit/contrib/wsgi_handler.py">code</a> is also a good example of use.</p>
    </article>
</section>

        
        <footer id="bottom">
            <p class="copyright">2008-2012 &copy; <a href="http://benoitc.org">Benoît Chesneau</a> - Some rights reserved.</p>
            <div id="cc"><a rel="license" href="http://creativecommons.org/licenses/by/2.0/fr/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by/2.0/fr/80x15.png" /></a> This <span xmlns:dc="http://purl.org/dc/elements/1.1/" href="http://purl.org/dc/dcmitype/InteractiveResource" rel="dc:type">website</span> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/2.0/fr/">Creative Commons Attribution 2.0 France License</a>.</div>
            <p>Hosted on <a href="http://github.com/">GitHub</a></p>
        </footer>
        
    </div>

    
    
    
</body>
</html>